#include <iostream>

using namespace std;

bool nextPermutation(int* permutation, int low, int high)
	{
		int i = low;
		for (i = high - 1; i >= low; --i) {
			if ((permutation[i] < permutation[i + 1]))
				break;
		}
		if (i < 0) {
			for (i = low; i <= high; i++) {
				permutation[i] = i + 1;
			}
			return false;
		} else {
			int latest = i + 1;
			for (int j = i + 2; j <= high; j++) {
				if (permutation[j] > permutation[i] && permutation[j] < permutation[latest]) {
					latest = j;
				}
			}
			// Swap the latest and permutation[i]
			int temp = permutation[latest];
			permutation[latest] = permutation[i];
			permutation[i] = temp;
			// Swap the remaining elements
			int halfLength = (high - i) / 2;
			for (int j = 0; j < halfLength; j++) {
				temp = permutation[j + i + 1];
				permutation[j + i + 1] = permutation[high - j];
				permutation[high - j] = temp;
			}			
			return true;
		}		
	}

int main(int argc, char *argv[])
{
	const int MAX = 1024;
	int permutation[MAX];
	int m, n, k;
	scanf("%d", &m);
	int i = 0;
	for (i = 0; i < m; i++) {
		scanf("%d %d", &n, &k);
		int temp;
		for (int j = 0;j < n; j++) {
			scanf("%d", &temp);
			permutation[j] = temp;
		}
		for (int j = 0; j < k; j++) {
			nextPermutation(permutation, 0, n - 1);
		}
		for (int j = 0; j < n; ++j) {
			printf("%d ", permutation[j]);
		}
		printf("\n");

	}
	return 0;
}

//#include <iostream>
//#include <vector>
//#include <algorithm>
//
//using namespace std;
//
//int main(int argc, char *argv[])
//{
//	int m, n, k;
//	scanf("%d", &m);
//	vector<int> permutation;
//	int i = 0;
//	for (i = 0; i < m; i++) {
//		scanf("%d %d", &n, &k);
//		permutation.clear();
//		int temp;
//		for (int j = 0;j < n; j++) {
//			scanf("%d", &temp);
//			permutation.push_back(temp);
//		}
//		bool result = false;
//		for (int j = 0; j < k; j++) {
//			next_permutation(permutation.begin(), permutation.end());
//		}
//		vector<int>::iterator iterator;
//		for (iterator = permutation.begin(); iterator != permutation.end(); iterator++) {
//			printf("%d ", *iterator);
//		}
//		printf("\n");
//
//	}
//	return 0;
//}
